﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using IMDTO;
using IMBL;
using System.Web.Security;

namespace IMPPFront
{
    public partial class PP : System.Web.UI.Page
    {
        private IMDTOPersoonPlan pplan;

        protected void Page_Load(object sender, EventArgs e)
        {
            //try
            //{
                Guid currentUserGuid = (Guid)Membership.GetUser().ProviderUserKey;
                IMDTONaturalPerson person = IMBLNaturalPerson.GetNaturalPersonForMembershipUser(currentUserGuid);
                if (!X.IsAjaxRequest || !IsPostBack)
                {
                    // Check if person id is set
                    if (person != null)
                    {
                        //// Person ID is set
                        //int personID = int.Parse(Request.QueryString["PersonID"].ToString());

                        //// Set name
                        //IMDTONaturalPerson person = IMBLPersPlan.GetNaturalPersonById(personID);
                        lblNaam.Text = person.Name.FirstName + " " + person.Name.LastName;
                        lblNaamPAP.Html = person.Name.FirstName + " " + person.Name.LastName + "<br />";
                        lblNaamPOP.Html = person.Name.FirstName + " " + person.Name.LastName + "<br />";

                        // Get persplan
                        pplan = IMBLPersPlan.GetPersoonPlanById(person.idNaturalPerson, 0);

                        // Set middelendomein
                        lblMiddelendomein.Text = IMBLRefCodeDetail.GetValueByRefCode(100, pplan.MiddelenDomein);

                        // Set prestatiebreuk
                        lblPrestatieBreuk.Text = pplan.PrestatieBreuk + " uur / week";

                        // Set periode
                        lblPeriode.Text = pplan.StartDate.ToShortDateString() + " tot " + pplan.EndDate.ToShortDateString();

                        //Put all Competenties & Kernresultaten into one list
                        List<IMDTOGedragsCompetentie> gedragsCompetenties = new List<IMDTOGedragsCompetentie>();
                        List<IMDTOTechnischeCompetentie> technischeCompetenties = new List<IMDTOTechnischeCompetentie>();
                        List<IMDTOKernResultaat> kernresultaten = new List<IMDTOKernResultaat>();
                        foreach (IMDTOFunctieRol frDTO in pplan.FunctieRollen)
                        {
                            gedragsCompetenties.AddRange(frDTO.GedragsCompetenties);
                            technischeCompetenties.AddRange(frDTO.TechnischeCompetenties);
                            kernresultaten.AddRange(frDTO.Kernresultaten);
                        }

                        List<IMDTOTechnischeCompetentie> techComp = (from tc in technischeCompetenties
                                                                     group tc by tc.Name
                                                                         into tcg
                                                                         select new IMDTOTechnischeCompetentie
                                                                         {
                                                                             Name = tcg.Key,
                                                                             Level = tcg.Max(tc => tc.Level)
                                                                         }).ToList();
                        List<IMDTOTechnischeCompetentie> gedragsComp = (from gc in gedragsCompetenties
                                                                        group gc by gc.Name
                                                                            into gcg
                                                                            select new IMDTOTechnischeCompetentie
                                                                            {
                                                                                Name = gcg.Key,
                                                                                Level = gcg.Max(gc => gc.Level)
                                                                            }).ToList();

                        //set data
                        FunctieRollenStore.DataSource = pplan.FunctieRollen;
                        FunctieRollenStore.DataBind();
                        AanvullendeFunctieRollenStore.DataSource = pplan.AanvullendeFunctieRollen;
                        AanvullendeFunctieRollenStore.DataBind();
                        TechnischeCompStore.DataSource = techComp;
                        TechnischeCompStore.DataBind();
                        GedragsCompStore.DataSource = gedragsComp;
                        GedragsCompStore.DataBind();
                        CreateKernresultatenTree(kernresultaten, "Globaal");

                        // Set json urls
                        //((HttpProxy)FunctieRollenStore.Proxy[0]).Url = "Services/FunctieRollen.ashx?ID=" + personID;
                        //((HttpProxy)AanvullendeFunctieRollenStore.Proxy[0]).Url = "Services/AanvullendeFunctieRollen.ashx?ID=" + personID;
                        //((TreeLoader)KernresultatenTreePanel.Loader[0]).DataUrl = "Services/Kernresultaten.ashx?ID=" + personID;
                        //((HttpProxy)TechnischeCompStore.Proxy[0]).Url = "Services/TechnischeCompetenties.ashx?ID=" + personID;
                        //((HttpProxy)GedragsCompStore.Proxy[0]).Url = "Services/GedragsCompetenties.ashx?ID=" + personID; ;
                    }
                    else
                    {
                        Response.Redirect("~/Error.aspx?Error=1");
                    }
                }
                else
                {
                    if (person != null)
                    {
                        // Person ID is set
                        pplan = IMBLPersPlan.GetPersoonPlanById(person.idNaturalPerson, 0);
                    }
                    else
                    {
                        Response.Redirect("~/Error.aspx?Error=1");
                    }
                }
            //}
            //catch (Exception exception)
            //{
            //    //Response.Redirect("~/Error.aspx");
            //    throw exception;
            //}
        }

        public void CreateKernresultatenTree(List<IMDTOKernResultaat> kernList, String rootName)
        {
            Ext.Net.TreeNode root = new Ext.Net.TreeNode(rootName);
            root.Expanded = true;
            KernresultatenTreePanel.Root.Insert(0, root);            

            //Get all KernresultaatGebieden
            List<String> krGebieden = (from kr in kernList
                                      select kr.KernresultaatGebied).Distinct().ToList();
            foreach(String kGebied in krGebieden)
            {
                Ext.Net.TreeNode gebiedNode = new Ext.Net.TreeNode(kGebied, Icon.BookTabs);
                root.Nodes.Add(gebiedNode);

                List<String> kernresultaten = (from kr in kernList
                                               where kr.KernresultaatGebied.Equals(kGebied)
                                               select kr.Kernresultaat1).Distinct().ToList();
                foreach (String kResultaat in kernresultaten)
                {
                    Ext.Net.TreeNode resultaatNode = new Ext.Net.TreeNode(kResultaat, Icon.TabBlue);
                    gebiedNode.Nodes.Add(resultaatNode);

                    List<String> kernTaken = (from kr in kernList
                                              where kr.KernresultaatGebied.Equals(kGebied) &&
                                              kr.Kernresultaat1.Equals(kResultaat)
                                              select kr.KernTaak).Distinct().ToList();
                    foreach (String kTaak in kernTaken)
                    {
                        Ext.Net.TreeNode taakNode = new Ext.Net.TreeNode(kTaak, Icon.PageWhiteMedal);
                        resultaatNode.Nodes.Add(taakNode);
                    }
                }
            }
            
      }

        public void FunctieRol_Select(object sender, DirectEventArgs e)
        {

            //int id = int.Parse(e.ExtraParams["id"]);
            int id = int.Parse(FunctieRollenRowSelectionModel.SelectedRow.RecordID);
            IMDTOFunctieRol selectedFunctieRol = (from fr in pplan.FunctieRollen
                                                  where fr.idFunctieRol == id
                                                  select fr).SingleOrDefault();

            List<IMDTOGedragsCompetentie> gedragsCompetenties = selectedFunctieRol.GedragsCompetenties;
            List<IMDTOTechnischeCompetentie> technischeCompetenties = selectedFunctieRol.TechnischeCompetenties;
            List<IMDTOKernResultaat> kernresultaten = selectedFunctieRol.Kernresultaten;
            TechnischeCompStore.DataSource = technischeCompetenties;
            TechnischeCompStore.DataBind();
            GedragsCompStore.DataSource = gedragsCompetenties;
            GedragsCompStore.DataBind();
            CreateKernresultatenTree(kernresultaten, selectedFunctieRol.Name);
        }
    }
}